package view.modifiers;

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.path.CSPlayerPath;


public class ShiftModifier extends JPanel implements Modifier,FocusListener {

	private JTextField minField;
	private JTextField secField;
	
	private CSPlayerPath path;
	private int index;
	
	public ShiftModifier(CSPlayerPath path_, int index_) {
		
		path = path_;
		index = index_;
		
		int min = (int)Math.floor(path.getNode(index).getTime()/60);
		int sec = (int)path.getNode(index).getTime()-60*min;
		minField = new JTextField(""+min,3);
		secField = new JTextField(""+sec,3);
		
		minField.addFocusListener(this);
		secField.addFocusListener(this);
		
		this.setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
//		this.setBorder(BorderFactory.cre
		this.add(Box.createHorizontalStrut(3));
		this.add(new JLabel("Wait until "));
		this.add(minField);
		this.add(new JLabel(" min "));
		this.add(secField);
		this.add(new JLabel(" sec."));
		this.add(Box.createHorizontalStrut(3));
	}
	
	public void modify() {
		try {
			path.waitUntil(index, 60*Double.parseDouble(minField.getText())+Double.parseDouble(secField.getText()));
		}
		catch (NumberFormatException e) {}
	}

	public void focusGained(FocusEvent e) {
		((JTextField)(e.getSource())).selectAll();
	}

	public void focusLost(FocusEvent arg0) {}

}
